<?php

/*
 * Edit by xinli
 * Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
 */
namespace app\admin\controller;

use think\facade\Log;
use app\user\model\UserModel;
use api\mall\model\StoreLogModel;
use api\mall\model\UserAuditModel;
use api\mall\model\UserLoginMonthModel;
use cmf\controller\AdminBaseController;
use api\mall\model\DailyTaskModel;
use api\user\model\UserScoreLogModel;
use app\portal\model\UserBehaviorModel;

/**
 * Description of StatisticController
 *
 * @author PC
 */
class StatisticController extends AdminBaseController
{

    /**
     * 用户数据
     * @adminMenu(
     *     'name'   => '用户数据',
     *     'parent' => 'admin/Statistic/Index',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '用户数据',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $userLoginMonthModel = new UserLoginMonthModel();

        // ****************************** 用户访问量 ******************************
        // 本月累计登录数
        $curMonVisitCount = $userLoginMonthModel->where('statistic_month', date('Ym'))->count();//->sum('login_count');
        $this->assign('login_log_count', $curMonVisitCount);
        // 环比上月
        $lastMonVisitCount = $userLoginMonthModel->where('statistic_month', date('Ym', strtotime('-1 month')))->count();//->sum('login_count');
        $changeRate = $lastMonVisitCount != 0 ? ($curMonVisitCount - $lastMonVisitCount) / $lastMonVisitCount * 100 : 0;
        $changeRate = round($changeRate, 2);
        $this->assign('login_log_is_rise', 0 < $changeRate);
        $this->assign('login_log_change_rate', abs($changeRate));
        // 按月统计访问量 取最近6个月
        $userLoginMonths = $userLoginMonthModel->field('statistic_month, count(*) as login_count')
            ->where('statistic_month', '>=', date('Ym', strtotime('-5 month')))
            ->where('statistic_month', '<=', date('Ym'))
            ->group('statistic_month')
            ->select();
        // 拆分访问量为时间数组和数据数组
        $months = [];
        $loginCounts = [];
        foreach ($userLoginMonths as &$loginLog) {
            array_push($months, $loginLog['statistic_month'] % 100);
            array_push($loginCounts, $loginLog['login_count']);
        }
        $this->assign('login_log_months', json_encode($months));
        $this->assign('login_log_counts', json_encode($loginCounts));

        // ****************************** 新增用户量 ******************************
        // 计算本月新增用户
        $curMonNewUserCount = UserModel::where('create_time', '>=', strtotime(date('Y-m-01')))
            ->where('create_time', '<', strtotime(date('Y-m-01', strtotime('+1 month'))))
            ->count();
        $this->assign('new_user_cur_mon_count', $curMonNewUserCount);    
        // 环比上月
        $lastMonNewUserCount = UserModel::where('create_time', '>=', strtotime(date('Y-m-01', strtotime('-1 month'))))
            ->where('create_time', '<', strtotime(date('Y-m-01')))
            ->count();
        $changeRate = $lastMonNewUserCount != 0 ? ($curMonNewUserCount - $lastMonNewUserCount) / $lastMonNewUserCount * 100 : 0;
        $changeRate = round($changeRate, 2);
        $this->assign('new_user_is_rise', 0 < $changeRate);
        $this->assign('new_user_change_rate', abs($changeRate));
        // 按月统计新增用户 取最近6个月
        $userLoginMonths = UserModel::field('FROM_UNIXTIME(create_time, "%m") as month, count(id) as user_count')
            ->where('create_time', '>=', strtotime(date('Y-m-01', strtotime('-5 month'))))
            ->where('create_time', '<', strtotime(date('Y-m-01', strtotime('+1 month'))))
            ->group('month')
            ->select();
        // 拆分访问量为时间数组和数据数组
        $months = [];
        $userCounts = [];
        foreach ($userLoginMonths as &$loginLog) {
            array_push($months, $loginLog['month']);
            array_push($userCounts, $loginLog['user_count']);
        }
        $this->assign('new_user_months', json_encode($months));
        $this->assign('new_user_counts', json_encode($userCounts));

        // ****************************** 月活用户数 ******************************
        // 本月月活用户数 每个user_id只记录一次
        $curMonActiveUserCount = $userLoginMonthModel->where('statistic_month', date('Ym'))->count();
        $this->assign('active_user_cur_mon_count', $curMonActiveUserCount);
        // 环比上月
        $lastMonActiveUserCount = $userLoginMonthModel->where('statistic_month', date('Ym', strtotime('-1 month')))->count();
        $changeRate = $lastMonActiveUserCount != 0 ? ($curMonActiveUserCount - $lastMonActiveUserCount) / $lastMonActiveUserCount * 100 : 0;
        $changeRate = round($changeRate, 2);
        $this->assign('active_user_is_rise', 0 < $changeRate);
        $this->assign('active_user_change_rate', abs($changeRate));
        // 按月统计月活用户数 取最近6个月
        $userLoginMonths = $userLoginMonthModel->field('statistic_month, count(id) as user_count')
            ->where('statistic_month', '>=', date('Ym', strtotime('-5 month')))
            ->where('statistic_month', '<=', date('Ym'))
            ->group('statistic_month')
            ->select();
        // 拆分访问量为时间数组和数据数组
        $months = [];
        $userCounts = [];
        foreach ($userLoginMonths as &$loginLog) {
            array_push($months, $loginLog['statistic_month'] % 100);
            array_push($userCounts, $loginLog['user_count']);
        }
        $this->assign('active_user_months', json_encode($months));
        $this->assign('active_user_counts', json_encode($userCounts));
        
        // ****************************** 日访问统计 ******************************
        // 累计用户数
        $totalUserCount = UserModel::count();
        $this->assign('total_user_count', $totalUserCount);
        // 今日新增用户数
        $todayNewUserCount = UserModel::where('create_time', '>=', strtotime(date('Y-m-d')))
            ->where('create_time', '<', strtotime(date('Y-m-d', strtotime('+1 day'))))
            ->count();
        $this->assign('today_new_user_count', $todayNewUserCount);
        // 近7日登录统计
        $login7DayCountArray = cmf_get_option('login_7day_count_array');
        if (empty($login7DayCountArray)) {
            $login7DayCountArray = [];
        }
        // 近7日新增人数 查询数据库
        $login7DayNewUserCountArray = [];
        for ($i = 6; 0 <= $i; $i--) {
            $date = date('Ymd', strtotime('-' . $i . ' day'));
            $login7DayNewUserCountArray[$date] = UserModel::where('create_time', '>=', strtotime(date('Y-m-d', strtotime('-' . $i . ' day'))))
                ->where('create_time', '<', strtotime(date('Y-m-d', strtotime('-' . $i . ' day'))) + 86400)
                ->count();
        }
        // 整理数据
        $asLogin7DayTitleArray = ['product'];
        $asLogin7DayCountArray = ['访问人数'];
        $asLogin7DayNewUserArray = ['新增人数'];        
        for ($i = 6; 0 <= $i; $i--) {
            $date = date('Ymd', strtotime('-' . $i . ' day'));
            array_push($asLogin7DayTitleArray, $date);
            array_push($asLogin7DayCountArray, isset($login7DayCountArray['day' . $date]) ? $login7DayCountArray['day' . $date] : 0);
            array_push($asLogin7DayNewUserArray, isset($login7DayNewUserCountArray[$date]) ? $login7DayNewUserCountArray[$date] : 0);
        }
        $this->assign('login_7day_title_array', json_encode($asLogin7DayTitleArray));   
        $this->assign('login_7day_count_array', json_encode($asLogin7DayCountArray));   
        $this->assign('login_7day_new_user_count_array', json_encode($asLogin7DayNewUserArray)); 

        // ****************************** 审核比例统计 ******************************
        // 审核用户百分比统计 包括已通过 待审核 已拒绝的审核比例统计 userauditmodel
//        $totalUserAuditCount = UserAuditModel::count();
        $userAuditModel = new UserAuditModel();
        $auditPassCount = $userAuditModel->where('audit_status', 1)->count();
        $auditWaitCount = $userAuditModel->where('audit_status', 0)->count();
        $auditRejectCount = $userAuditModel->where('audit_status', 2)->count();
        $totalUserAuditCount = $auditPassCount+$auditWaitCount+$auditRejectCount;
        if($totalUserAuditCount<1){
            $totalUserAuditCount=1;
        }
        $this->assign('audit_pass_rate',  round($auditPassCount / $totalUserAuditCount * 100, 2));
        $this->assign('audit_wait_rate',  round($auditWaitCount / $totalUserAuditCount * 100, 2));
        $this->assign('audit_reject_rate',  round($auditRejectCount / $totalUserAuditCount * 100, 2));

        // ****************************** 用户排行榜 ******************************
        // 检索用户表 以login_day_count降序排列
        $model = new UserModel();
        $query = $model->alias('cu');
        $query->leftJoin('cmf_store_log csl', 'csl.user_id = cu.id');
        
        $sortCol = $this->request->param('sort_col', 3);
        $sortType = $this->request->param('sort_type', 'desc');
        if ($sortCol == 3) {
            $query->order('login_day_count', $sortType);
        }
        else if ($sortCol == 4) {
            $query->order('order_count', $sortType);
        }
        else if ($sortCol == 5) {
            $query->order('order_sum', $sortType);
        }
        else {
            $query->order('login_day_count', $sortType);
        }
        $query->order('id asc');
        
        $userRankList = $query->field('cu.*, COALESCE(COUNT(csl.user_id), 0) AS order_count, COALESCE(SUM(csl.cost), 0) AS order_sum')
            ->group('cu.id')->limit(10)->select();

        $this->assign('sort_col', $sortCol);
        $this->assign('sort_type', $sortType);
        $this->assign('user_rank_list', $userRankList);

        // ****************************** 消费监控 ******************************
        $storeLogModel = new StoreLogModel();
        $query = $storeLogModel->alias('csl');
        $query->leftJoin('cmf_user cu', 'cu.id = csl.user_id');
        $query->leftJoin('cmf_store_item csi', 'csi.id = csl.item_id');
        $consumerMonitoringList = $query->field('csl.*, cu.avatar, cu.user_nickname, cu.mobile, csi.item_icon')
            ->order('csl.create_time', 'desc')->limit(10)->select();
        $this->assign('consumer_monitoring_list', $consumerMonitoringList);

        return $this->fetch('index');
    }
    
    /**
     * 数据概况
     * @adminMenu(
     *     'name'   => '数据概况',
     *     'parent' => 'admin/Statistic/Overview',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '数据概况',
     *     'param'  => ''
     * )
     */
    public function overview()
    {
        $userLoginMonthModel = new UserLoginMonthModel();

        // ****************************** 销售额分类占比 ******************************
        $storeLogModel = new StoreLogModel();
        $query = $storeLogModel->alias('csl');
        $query->leftJoin('cmf_store_item csi', 'csi.id = csl.item_id');
        $saleData = $query->field('sum(csl.item_num) as sale_num, csi.item_type')
            ->group('csi.item_type')->select();
        $totalSaleNum = 0;
        foreach ($saleData as $item) {
            $totalSaleNum += $item['sale_num'];
        }
        $salesClassificationProportion = [];
        foreach ($saleData as &$item) {
            $temp['value'] = round($item['sale_num'] / $totalSaleNum * 100, 2);
            $temp['name'] = $item['item_type'] . ' ' . $temp['value'] . '%';
            array_push($salesClassificationProportion, $temp);
        }        
        $this->assign('sales_classification_proportion', json_encode($salesClassificationProportion));


        // ****************************** 销售漏斗 ******************************
        // 平台总用户数
        $totalUserCount = UserModel::count();
        // 浏览商品的用户数
        $browseUserCount = UserBehaviorModel::where('browse', 1)->count();
        // 加入购物车的用户数
        $addCartUserCount = UserBehaviorModel::where('add_cart', 1)->count();
        // 下单的用户数
        $orderUserCount = UserBehaviorModel::where('create_order', 1)->count();
        // 支付的用户数
        $payUserCount = UserBehaviorModel::where('pay_order', 1)->count();
        // 完成交易的用户数
        $completeUserCount = UserBehaviorModel::where('complete_deal', 1)->count();
        // 组装数据
        $salesFunnel = [];
        $percent = $totalUserCount == 0 ? 0 : round($browseUserCount / $totalUserCount * 100, 2);
        $browse['value'] = $browseUserCount;
        $browse['name'] = '浏览商品 ' . $browseUserCount . ' ' . $percent . '%';
        array_push($salesFunnel, $browse);
        $percent = $browseUserCount == 0 ? 0 : round($addCartUserCount / $browseUserCount * 100, 2);
        $addCart['value'] = $addCartUserCount;
        $addCart['name'] = '放购物车 ' . $addCartUserCount . ' ' . $percent . '%';
        array_push($salesFunnel, $addCart);
        $percent = $addCartUserCount == 0 ? 0 : round($orderUserCount / $addCartUserCount * 100, 2);
        $order['value'] = $orderUserCount;
        $order['name'] = '生成订单 ' . $orderUserCount . ' ' . $percent . '%';
        array_push($salesFunnel, $order);
        $percent = $orderUserCount == 0 ? 0 : round($payUserCount / $orderUserCount * 100, 2);
        $pay['value'] = $payUserCount;
        $pay['name'] = '支付订单 ' . $payUserCount . ' ' . $percent . '%';
        array_push($salesFunnel, $pay);
        $percent = $payUserCount == 0 ? 0 : round($completeUserCount / $payUserCount * 100, 2);
        $complete['value'] = $completeUserCount;
        $complete['name'] = '完成交易 ' . $completeUserCount . ' ' . $percent . '%';
        array_push($salesFunnel, $complete);
        $this->assign('sales_funnel', json_encode($salesFunnel));


        // ****************************** 藏品累计销售额 ******************************
        $storeLogModel = new StoreLogModel();
        // 统计销售额总和
        $totalCollectionSaleSum = $storeLogModel->where('status', '>', 0)->where('booth_type', 1)->sum('cost');
        $this->assign('total_collection_sale_sum', $totalCollectionSaleSum);
        $totalProductSaleSum = $storeLogModel->where('status', '>', 0)->where('booth_type', '<>', 1)->sum('cost');
        $this->assign('total_product_sale_sum', $totalProductSaleSum);
        // ****************************** 统计月维度下的
        $yearMonth = ['月份', '1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'];
        $this->assign('year_month_date', json_encode($yearMonth));
        // 本年度/上年度 藏品销量总额
        $thisYearSaleData = $storeLogModel->field('sum(cost) as sale_sum, DATE_FORMAT(FROM_UNIXTIME(create_time), "%m") as month')
            ->where('status', '>', 0)
            ->where('booth_type', 1)
            ->where('create_time', '>=', 'UNIX_TIMESTAMP(DATE_FORMAT(NOW(), "%Y-01-01 00:00:00"))')
            ->group('month')->select();
        $tempSaleData = [];
        foreach ($thisYearSaleData as $item) {
            $item['month'] = intval($item['month']);
            $tempSaleData[$item['month']] = $item['sale_sum'];
        }
        $thisYearMonthSaleData = ['本年度藏品销售额'];
        for ($i = 1; $i <= 12; $i++) {
            if (!isset($tempSaleData[$i])) {
                $tempSaleData[$i] = 0;
            }
            array_push($thisYearMonthSaleData, $tempSaleData[$i]);
        }
        $this->assign('this_year_collection_sale_data', json_encode($thisYearMonthSaleData));
        $lastYearSaleData = $storeLogModel->field('sum(cost) as sale_sum, DATE_FORMAT(FROM_UNIXTIME(create_time), "%m") as month')
            ->where('status', '>', 0)
            ->where('booth_type', 1)
            ->where('create_time', '<', 'UNIX_TIMESTAMP(DATE_FORMAT(NOW(), "%Y-01-01 00:00:00"))')
            ->where('create_time', '>=', 'UNIX_TIMESTAMP(DATE_SUB(DATE_FORMAT(NOW(), "%Y-01-01 00:00:00"), INTERVAL 1 YEAR))')
            ->group('month')->select();
        $tempSaleData = [];
        foreach ($lastYearSaleData as $item) {
            $item['month'] = intval($item['month']);
            $tempSaleData[$item['month']] = $item['sale_sum'];
        }
        $lastYearMonthSaleData = ['上年度藏品销售额'];
        for ($i = 1; $i <= 12; $i++) {
            if (!isset($tempSaleData[$i])) {
                $tempSaleData[$i] = 0;
            }
            array_push($lastYearMonthSaleData, $tempSaleData[$i]);
        }
        $this->assign('last_year_collection_sale_data', json_encode($lastYearMonthSaleData));
        // 本年度/上年度 商品销量总额
        $thisYearSaleData = $storeLogModel->field('sum(cost) as sale_sum, DATE_FORMAT(FROM_UNIXTIME(create_time), "%m") as month')
            ->where('status', '>', 0)
            ->where('booth_type', '<>', 1)
            ->where('create_time', '>=', 'UNIX_TIMESTAMP(DATE_FORMAT(NOW(), "%Y-01-01 00:00:00"))')
            ->group('month')->select();
        $tempSaleData = [];
        foreach ($thisYearSaleData as $item) {
            $item['month'] = intval($item['month']);
            $tempSaleData[$item['month']] = $item['sale_sum'];
        }
        $thisYearMonthSaleData = ['本年度商品销售额'];
        for ($i = 1; $i <= 12; $i++) {
            if (!isset($tempSaleData[$i])) {
                $tempSaleData[$i] = 0;
            }
            array_push($thisYearMonthSaleData, $tempSaleData[$i]);
        }
        $this->assign('this_year_product_sale_data', json_encode($thisYearMonthSaleData));
        $lastYearSaleData = $storeLogModel->field('sum(cost) as sale_sum, DATE_FORMAT(FROM_UNIXTIME(create_time), "%m") as month')
            ->where('status', '>', 0)
            ->where('booth_type', '<>', 1)
            ->where('create_time', '<', 'UNIX_TIMESTAMP(DATE_FORMAT(NOW(), "%Y-01-01 00:00:00"))')
            ->where('create_time', '>=', 'UNIX_TIMESTAMP(DATE_SUB(DATE_FORMAT(NOW(), "%Y-01-01 00:00:00"), INTERVAL 1 YEAR))')
            ->group('month')->select();
        $tempSaleData = [];
        foreach ($lastYearSaleData as $item) {
            $item['month'] = intval($item['month']);
            $tempSaleData[$item['month']] = $item['sale_sum'];
        }
        $lastYearMonthSaleData = ['上年度商品销售额'];
        for ($i = 1; $i <= 12; $i++) {
            if (!isset($tempSaleData[$i])) {
                $tempSaleData[$i] = 0;
            }
            array_push($lastYearMonthSaleData, $tempSaleData[$i]);
        }
        $this->assign('last_year_product_sale_data', json_encode($lastYearMonthSaleData));

        // ****************************** 统计周维度下的
        $weekDate = ['周', '周一', '周二', '周三', '周四', '周五', '周六', '周日'];
        $this->assign('week_date', json_encode($weekDate));
        // 计算本周周一0点时间戳
        $thisMonday = strtotime(date('Y-m-d', strtotime('this week')));
        // 计算上周周一0点时间戳
        $lastMonday = strtotime(date('Y-m-d', strtotime('last week')));
        //  本周/上周 藏品销量总额
        $thisWeekSaleData = $storeLogModel->field('sum(cost) as sale_sum, DAYOFWEEK(FROM_UNIXTIME(create_time)) as week')
            ->where('status', '>', 0)
            ->where('booth_type', 1)
            ->where('create_time', '>=', $thisMonday)
            ->group('week')->select();
        $tempSaleData = [];
        foreach ($thisWeekSaleData as $item) {
            $item['week'] = intval($item['week']) - 1;
            $item['week'] = $item['week'] == 0 ? 7 : $item['week'];
            $tempSaleData[$item['week']] = $item['sale_sum'];
        }
        $thisWeekSaleData = ['本周藏品销售额'];
        for ($i = 1; $i <= 7; $i++) {
            if (!isset($tempSaleData[$i])) {
                $tempSaleData[$i] = 0;
            }
            array_push($thisWeekSaleData, $tempSaleData[$i]);
        }
        $this->assign('this_week_collection_sale_data', json_encode($thisWeekSaleData));
        $lastWeekSaleData = $storeLogModel->field('sum(cost) as sale_sum, DAYOFWEEK(FROM_UNIXTIME(create_time)) as week')
            ->where('status', '>', 0)
            ->where('booth_type', 1)
            ->where('create_time', '<', $thisMonday)
            ->where('create_time', '>=', $lastMonday)
            ->group('week')->select();
        $tempSaleData = [];
        foreach ($lastWeekSaleData as $item) {
            $item['week'] = intval($item['week']) - 1;
            $item['week'] = $item['week'] == 0 ? 7 : $item['week'];
            $tempSaleData[$item['week']] = $item['sale_sum'];
        }
        $lastWeekSaleData = ['上周藏品销售额'];
        for ($i = 1; $i <= 7; $i++) {
            if (!isset($tempSaleData[$i])) {
                $tempSaleData[$i] = 0;
            }
            array_push($lastWeekSaleData, $tempSaleData[$i]);
        }
        $this->assign('last_week_collection_sale_data', json_encode($lastWeekSaleData));
        // 本周/上周 商品销量总额
        $thisWeekSaleData = $storeLogModel->field('sum(cost) as sale_sum, DAYOFWEEK(FROM_UNIXTIME(create_time)) as week')
            ->where('status', '>', 0)
            ->where('booth_type', '<>', 1)
            ->where('create_time', '>=', $thisMonday)
            ->group('week')->select();
        $tempSaleData = [];
        foreach ($thisWeekSaleData as $item) {
            $item['week'] = intval($item['week']) - 1;
            $item['week'] = $item['week'] == 0 ? 7 : $item['week'];
            $tempSaleData[$item['week']] = $item['sale_sum'];
        }
        $thisWeekSaleData = ['本周商品销售额'];
        for ($i = 1; $i <= 7; $i++) {
            if (!isset($tempSaleData[$i])) {
                $tempSaleData[$i] = 0;
            }
            array_push($thisWeekSaleData, $tempSaleData[$i]);
        }
        $this->assign('this_week_product_sale_data', json_encode($thisWeekSaleData));
        $lastWeekSaleData = $storeLogModel->field('sum(cost) as sale_sum, DAYOFWEEK(FROM_UNIXTIME(create_time)) as week')
            ->where('status', '>', 0)
            ->where('booth_type', '<>', 1)
            ->where('create_time', '<', $thisMonday)
            ->where('create_time', '>=', $lastMonday)
            ->group('week')->select();
        $tempSaleData = [];
        foreach ($lastWeekSaleData as $item) {
            $item['week'] = intval($item['week']) - 1;
            $item['week'] = $item['week'] == 0 ? 7 : $item['week'];
            $tempSaleData[$item['week']] = $item['sale_sum'];
        }
        $lastWeekSaleData = ['上周商品销售额'];
        for ($i = 1; $i <= 7; $i++) {
            if (!isset($tempSaleData[$i])) {
                $tempSaleData[$i] = 0;
            }
            array_push($lastWeekSaleData, $tempSaleData[$i]);
        }
        $this->assign('last_week_product_sale_data', json_encode($lastWeekSaleData));
        
        // ****************************** 统计日维度下的
        $dayDate = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', 
            '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31'];
        $this->assign('day_date', json_encode($dayDate));
        // 计算本月1日0点时间戳
        $thisFirstDay = strtotime(date('Y-m-01'));
        // 计算上月1日0点时间戳
        $lastFirstDay = strtotime(date('Y-m-01', strtotime('-1 month')));
        //  本月/上月 藏品销量总额
        $thisDaySaleData = $storeLogModel->field('sum(cost) as sale_sum, DAYOFMONTH(FROM_UNIXTIME(create_time)) as day')
            ->where('status', '>', 0)
            ->where('booth_type', 1)
            ->where('create_time', '>=', $thisFirstDay)
            ->group('day')->select();
        $tempSaleData = [];
        foreach ($thisDaySaleData as $item) {
            $item['day'] = intval($item['day']);
            $tempSaleData[$item['day']] = $item['sale_sum'];
        }
        $thisDaySaleData = ['本月藏品销售额'];
        for ($i = 1; $i <= 31; $i++) {
            if (!isset($tempSaleData[$i])) {
                $tempSaleData[$i] = 0;
            }
            array_push($thisDaySaleData, $tempSaleData[$i]);
        }
        $this->assign('this_day_collection_sale_data', json_encode($thisDaySaleData));
        $lastDaySaleData = $storeLogModel->field('sum(cost) as sale_sum, DAYOFMONTH(FROM_UNIXTIME(create_time)) as day')
            ->where('status', '>', 0)
            ->where('booth_type', 1)
            ->where('create_time', '<', $thisFirstDay)
            ->where('create_time', '>=', $lastFirstDay)
            ->group('day')->select();
        $tempSaleData = [];
        foreach ($lastDaySaleData as $item) {
            $item['day'] = intval($item['day']);
            $tempSaleData[$item['day']] = $item['sale_sum'];
        }
        $lastDaySaleData = ['上月藏品销售额'];
        for ($i = 1; $i <= 31; $i++) {
            if (!isset($tempSaleData[$i])) {
                $tempSaleData[$i] = 0;
            }
            array_push($lastDaySaleData, $tempSaleData[$i]);
        }
        $this->assign('last_day_collection_sale_data', json_encode($lastDaySaleData));
        //  本月/上月 商品销量总额
        $thisDaySaleData = $storeLogModel->field('sum(cost) as sale_sum, DAYOFMONTH(FROM_UNIXTIME(create_time)) as day')
            ->where('status', '>', 0)
            ->where('booth_type', '<>', 1)
            ->where('create_time', '>=', $thisFirstDay)
            ->group('day')->select();
        $tempSaleData = [];
        foreach ($thisDaySaleData as $item) {
            $item['day'] = intval($item['day']);
            $tempSaleData[$item['day']] = $item['sale_sum'];
        }
        $thisDaySaleData = ['本月商品销售额'];
        for ($i = 1; $i <= 31; $i++) {
            if (!isset($tempSaleData[$i])) {
                $tempSaleData[$i] = 0;
            }
            array_push($thisDaySaleData, $tempSaleData[$i]);
        }
        $this->assign('this_day_product_sale_data', json_encode($thisDaySaleData));
        $lastDaySaleData = $storeLogModel->field('sum(cost) as sale_sum, DAYOFMONTH(FROM_UNIXTIME(create_time)) as day')
            ->where('status', '>', 0)
            ->where('booth_type', '<>', 1)
            ->where('create_time', '<', $thisFirstDay)
            ->where('create_time', '>=', $lastFirstDay)
            ->group('day')->select();
        $tempSaleData = [];
        foreach ($lastDaySaleData as $item) {
            $item['day'] = intval($item['day']);
            $tempSaleData[$item['day']] = $item['sale_sum'];
        }
        $lastDaySaleData = ['上月商品销售额'];
        for ($i = 1; $i <= 31; $i++) {
            if (!isset($tempSaleData[$i])) {
                $tempSaleData[$i] = 0;
            }
            array_push($lastDaySaleData, $tempSaleData[$i]);
        }
        $this->assign('last_day_product_sale_data', json_encode($lastDaySaleData));
        
        // ****************************** 细分各板块数据 ******************************
        // 计算累计销售额
        $totalSaleSum = $storeLogModel->where('status', '>', 0)->sum('cost');
        $this->assign('total_sale_sum', $totalSaleSum);
        // 计算今日比昨日增加销售额
        $todaySaleSum = $storeLogModel->where('status', '>', 0)->where('create_time', '>=', strtotime(date('Y-m-d')))
            ->where('create_time', '<', strtotime(date('Y-m-d', strtotime('+1 day'))))->sum('cost');
        $yesterdaySaleSum = $storeLogModel->where('status', '>', 0)->where('create_time', '>=', strtotime(date('Y-m-d', strtotime('-1 day'))))
            ->where('create_time', '<', strtotime(date('Y-m-d')))->sum('cost');
        // $this->assign('today_sale_sum', $todaySaleSum);
        // $this->assign('yesterday_sale_sum', $yesterdaySaleSum);    
        $this->assign('today_sale_change', $todaySaleSum - $yesterdaySaleSum);
        // 计算累计订单数
        $totalOrderCount = $storeLogModel->where('status', '>', 0)->where('group_id', '<>', '')->group('group_id')->count();
        $totalOrderCount += $storeLogModel->where('status', '>', 0)->where('group_id', '')->count();
        $this->assign('total_order_count', $totalOrderCount);
        // 计算今日比昨日增加订单数
        $todayOrderCount = 
            $storeLogModel->where('status', '>', 0)->where('create_time', '>=', strtotime(date('Y-m-d')))
            ->where('create_time', '<', strtotime(date('Y-m-d', strtotime('+1 day'))))
            ->where('group_id', '<>', '')->group('group_id')->count();
        $todayOrderCount += 
            $storeLogModel->where('status', '>', 0)->where('create_time', '>=', strtotime(date('Y-m-d')))
            ->where('create_time', '<', strtotime(date('Y-m-d', strtotime('+1 day'))))
            ->where('group_id', '')->count();
        $yesterdayOrderCount = 
            $storeLogModel->where('status', '>', 0)->where('create_time', '>=', strtotime(date('Y-m-d', strtotime('-1 day'))))
            ->where('create_time', '<', strtotime(date('Y-m-d')))
            ->where('group_id', '<>', '')->group('group_id')->count();
        $yesterdayOrderCount += 
            $storeLogModel->where('status', '>', 0)->where('create_time', '>=', strtotime(date('Y-m-d', strtotime('-1 day'))))
            ->where('create_time', '<', strtotime(date('Y-m-d')))
            ->where('group_id', '')->count();
        $this->assign('today_order_change', $todayOrderCount - $yesterdayOrderCount);
        // 计算累计商品销量
        $totalProductNum = $storeLogModel->where('booth_type', '<>', 1)->where('status', '>', 0)->sum('item_num');
        $this->assign('total_product_num', $totalProductNum);
        // 计算今日比昨日增加商品销量
        $todayProductNum = $storeLogModel->where('booth_type', '<>', 1)->where('status', '>', 0)
            ->where('create_time', '>=', strtotime(date('Y-m-d')))
            ->where('create_time', '<', strtotime(date('Y-m-d', strtotime('+1 day'))))->sum('item_num');
        $yesterdayProductNum = $storeLogModel->where('booth_type', '<>', 1)->where('status', '>', 0)
            ->where('create_time', '>=', strtotime(date('Y-m-d', strtotime('-1 day'))))
            ->where('create_time', '<', strtotime(date('Y-m-d')))->sum('item_num');
        $this->assign('today_product_change', $todayProductNum - $yesterdayProductNum);
        // 计算累计商品销售额
        $totalProductSaleSum = $storeLogModel->where('booth_type', '<>', 1)->where('status', '>', 0)->sum('cost');
        $this->assign('total_product_sale_sum', $totalProductSaleSum);
        // 计算今日比昨日增加商品销售额
        $todayProductSaleSum = $storeLogModel->where('booth_type', '<>', 1)->where('status', '>', 0)
            ->where('create_time', '>=', strtotime(date('Y-m-d')))
            ->where('create_time', '<', strtotime(date('Y-m-d', strtotime('+1 day'))))->sum('cost');
        $yesterdayProductSaleSum = $storeLogModel->where('booth_type', '<>', 1)->where('status', '>', 0)
            ->where('create_time', '>=', strtotime(date('Y-m-d', strtotime('-1 day'))))
            ->where('create_time', '<', strtotime(date('Y-m-d')))->sum('cost');
        $this->assign('today_product_sale_change', $todayProductSaleSum - $yesterdayProductSaleSum);
        // 计算累计藏品销量
        $totalCollectionNum = $storeLogModel->where('booth_type', 1)->where('status', '>', 0)->sum('item_num');
        $this->assign('total_collection_num', $totalCollectionNum);
        // 计算今日比昨日增加藏品销量
        $todayCollectionNum = $storeLogModel->where('booth_type', 1)->where('status', '>', 0)
            ->where('create_time', '>=', strtotime(date('Y-m-d')))
            ->where('create_time', '<', strtotime(date('Y-m-d', strtotime('+1 day'))))->sum('item_num');
        $yesterdayCollectionNum = $storeLogModel->where('booth_type', 1)->where('status', '>', 0)
            ->where('create_time', '>=', strtotime(date('Y-m-d', strtotime('-1 day'))))
            ->where('create_time', '<', strtotime(date('Y-m-d')))->sum('item_num');
        $this->assign('today_collection_change', $todayCollectionNum - $yesterdayCollectionNum);
        // 计算累计藏品销售额
        $totalCollectionSaleSum = $storeLogModel->where('booth_type', 1)->where('status', '>', 0)->sum('cost');
        $this->assign('total_collection_sale_sum', $totalCollectionSaleSum);
        // 计算今日比昨日增加藏品销售额
        $todayCollectionSaleSum = $storeLogModel->where('booth_type', 1)->where('status', '>', 0)
            ->where('create_time', '>=', strtotime(date('Y-m-d')))
            ->where('create_time', '<', strtotime(date('Y-m-d', strtotime('+1 day'))))->sum('cost');
        $yesterdayCollectionSaleSum = $storeLogModel->where('booth_type', 1)->where('status', '>', 0)
            ->where('create_time', '>=', strtotime(date('Y-m-d', strtotime('-1 day'))))
            ->where('create_time', '<', strtotime(date('Y-m-d')))->sum('cost');
        $this->assign('today_collection_sale_change', $todayCollectionSaleSum - $yesterdayCollectionSaleSum);


        // ****************************** 订单数据统计 ******************************
        // ****************************** 统计月维度下的
        $yearMonth12 = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'];
        $this->assign('year_month_date_12', json_encode($yearMonth12));
        // 本年度 藏品销售订单数
        $tempSaleData = [];
        for ($i = 1; $i <= 12; $i++) {
            $tempSaleData[$i] = 0;
        }
        $thisYearSaleData1 = $storeLogModel->field('DATE_FORMAT(FROM_UNIXTIME(create_time), "%m") as month')
            ->where('status', '>', 0)
            ->where('booth_type', 1)
            ->where('create_time', '>=', 'UNIX_TIMESTAMP(DATE_FORMAT(NOW(), "%Y-01-01 00:00:00"))')
            ->where('group_id', '<>', '')
            ->group('group_id')->select();
        foreach ($thisYearSaleData1 as $item) {
            $item['month'] = intval($item['month']);
            $tempSaleData[$item['month']] += 1;
        }            
        $thisYearSaleData2 = $storeLogModel->field('count(*) as sale_num, DATE_FORMAT(FROM_UNIXTIME(create_time), "%m") as month')
            ->where('status', '>', 0)
            ->where('booth_type', 1)
            ->where('create_time', '>=', 'UNIX_TIMESTAMP(DATE_FORMAT(NOW(), "%Y-01-01 00:00:00"))')
            ->where('group_id', '')
            ->group('month')->select();
        foreach ($thisYearSaleData2 as $item) {
            $item['month'] = intval($item['month']);
            $tempSaleData[$item['month']] += $item['sale_num'];
        }
        $thisYearMonthSaleNum = [];
        for ($i = 1; $i <= 12; $i++) {
            array_push($thisYearMonthSaleNum, $tempSaleData[$i]);
        }
        $this->assign('this_year_collection_sale_num', json_encode($thisYearMonthSaleNum));
        // 本年度 商品销售订单数
        $tempSaleData = [];
        for ($i = 1; $i <= 12; $i++) {
            $tempSaleData[$i] = 0;
        }
        $thisYearSaleData1 = $storeLogModel->field('DATE_FORMAT(FROM_UNIXTIME(create_time), "%m") as month')
            ->where('status', '>', 0)
            ->where('booth_type', '<>', 1)
            ->where('create_time', '>=', 'UNIX_TIMESTAMP(DATE_FORMAT(NOW(), "%Y-01-01 00:00:00"))')
            ->where('group_id', '<>', '')
            ->group('group_id')->select();
        foreach ($thisYearSaleData1 as $item) {
            $item['month'] = intval($item['month']);
            $tempSaleData[$item['month']] += 1;
        }            
        $thisYearSaleData2 = $storeLogModel->field('count(*) as sale_num, DATE_FORMAT(FROM_UNIXTIME(create_time), "%m") as month')
            ->where('status', '>', 0)
            ->where('booth_type', '<>', 1)
            ->where('create_time', '>=', 'UNIX_TIMESTAMP(DATE_FORMAT(NOW(), "%Y-01-01 00:00:00"))')
            ->where('group_id', '')
            ->group('month')->select();
        foreach ($thisYearSaleData2 as $item) {
            $item['month'] = intval($item['month']);
            $tempSaleData[$item['month']] += $item['sale_num'];
        }
        $thisYearMonthSaleNum = [];   
        for ($i = 1; $i <= 12; $i++) {
            array_push($thisYearMonthSaleNum, $tempSaleData[$i]);
        }
        $this->assign('this_year_product_sale_num', json_encode($thisYearMonthSaleNum));
        // ****************************** 统计周维度下的
        $weekDate7 = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'];
        $this->assign('week_date7', json_encode($weekDate7));
        //  本周 藏品销售订单数
        $thisWeekSaleData = $storeLogModel->field('count(*) as sale_num, DAYOFWEEK(FROM_UNIXTIME(create_time)) as week')
            ->where('status', '>', 0)
            ->where('booth_type', 1)
            ->where('create_time', '>=', $thisMonday)
            ->group('group_id')->group('week')->select();
        $tempSaleData = [];
        foreach ($thisWeekSaleData as $item) {
            $item['week'] = intval($item['week']) - 1;
            $item['week'] = $item['week'] == 0 ? 7 : $item['week'];
            $tempSaleData[$item['week']] = 0;
            $tempSaleData[$item['week']] += $item['sale_num'];
        }
        $thisWeekSaleData = [];
        for ($i = 1; $i <= 7; $i++) {
            if (!isset($tempSaleData[$i])) {
                $tempSaleData[$i] = 0;
            }
            array_push($thisWeekSaleData, $tempSaleData[$i]);
        }
        $this->assign('this_week_collection_sale_num', json_encode($thisWeekSaleData));
        // 本周 商品销售订单数
        $thisWeekSaleData = $storeLogModel->field('count(*) as sale_num, DAYOFWEEK(FROM_UNIXTIME(create_time)) as week')
            ->where('status', '>', 0)
            ->where('booth_type', '<>', 1)
            ->where('create_time', '>=', $thisMonday)
            ->group('group_id')->group('week')->select();
        $tempSaleData = [];
        foreach ($thisWeekSaleData as $item) {
            $item['week'] = intval($item['week']) - 1;
            $item['week'] = $item['week'] == 0 ? 7 : $item['week'];
            $tempSaleData[$item['week']] = 0;
            $tempSaleData[$item['week']] += $item['sale_num'];
        }
        $thisWeekSaleData = [];
        for ($i = 1; $i <= 7; $i++) {
            if (!isset($tempSaleData[$i])) {
                $tempSaleData[$i] = 0;
            }
            array_push($thisWeekSaleData, $tempSaleData[$i]);
        }
        $this->assign('this_week_product_sale_num', json_encode($thisWeekSaleData));


        // ****************************** 销量排行榜 ******************************
        // 今日藏品销量排行榜
        $todayCollectionSaleRank = $storeLogModel->alias('csl')
            ->leftJoin('cmf_store_item csi', 'csi.id = csl.item_id')
            ->field('sum(csl.item_num) as sale_num, csi.item_name, csi.item_icon, csi.id')
            ->where('csl.status', '>', 0)
            ->where('csl.booth_type', 1)
            ->where('csl.create_time', '>=', strtotime(date('Y-m-d')))
            ->group('csl.item_id')
            ->order('sale_num', 'desc')
            ->limit(20)
            ->select();
        foreach ($todayCollectionSaleRank as &$item) {
            $item['item_icon'] = cmf_get_image_preview_url($item['item_icon']);
        }
        $this->assign('today_collection_sale_rank', $todayCollectionSaleRank);
        // 今日商品销量排行榜
        $todayProductSaleRank = $storeLogModel->alias('csl')
            ->leftJoin('cmf_store_item csi', 'csi.id = csl.item_id')
            ->field('sum(csl.item_num) as sale_num, csi.item_name, csi.item_icon, csi.id')
            ->where('csl.status', '>', 0)
            ->where('csl.booth_type', '<>', 1)
            ->where('csl.create_time', '>=', strtotime(date('Y-m-d')))
            ->group('csl.item_id')
            ->order('sale_num', 'desc')
            ->limit(20)
            ->select();
        foreach ($todayProductSaleRank as &$item) {
            $item['item_icon'] = cmf_get_image_preview_url($item['item_icon']);
        }
        $this->assign('today_product_sale_rank', $todayProductSaleRank);
        // 本周藏品销量排行榜
        $thisWeekCollectionSaleRank = $storeLogModel->alias('csl')
            ->leftJoin('cmf_store_item csi', 'csi.id = csl.item_id')
            ->field('sum(csl.item_num) as sale_num, csi.item_name, csi.item_icon, csi.id')
            ->where('csl.status', '>', 0)
            ->where('csl.booth_type', 1)
            ->where('csl.create_time', '>=', $thisMonday)
            ->group('csl.item_id')
            ->order('sale_num', 'desc')
            ->limit(20)
            ->select();
        foreach ($thisWeekCollectionSaleRank as &$item) {
            $item['item_icon'] = cmf_get_image_preview_url($item['item_icon']);
        }
        $this->assign('this_week_collection_sale_rank', $thisWeekCollectionSaleRank);
        // 本周商品销量排行榜
        $thisWeekProductSaleRank = $storeLogModel->alias('csl')
            ->leftJoin('cmf_store_item csi', 'csi.id = csl.item_id')
            ->field('sum(csl.item_num) as sale_num, csi.item_name, csi.item_icon, csi.id')
            ->where('csl.status', '>', 0)
            ->where('csl.booth_type', '<>', 1)
            ->where('csl.create_time', '>=', $thisMonday)
            ->group('csl.item_id')
            ->order('sale_num', 'desc')
            ->limit(20)
            ->select();
        foreach ($thisWeekProductSaleRank as &$item) {
            $item['item_icon'] = cmf_get_image_preview_url($item['item_icon']);
        }
        $this->assign('this_week_product_sale_rank', $thisWeekProductSaleRank);
        // 本月藏品销量排行榜
        $thisMonthCollectionSaleRank = $storeLogModel->alias('csl')
            ->leftJoin('cmf_store_item csi', 'csi.id = csl.item_id')
            ->field('sum(csl.item_num) as sale_num, csi.item_name, csi.item_icon, csi.id')
            ->where('csl.status', '>', 0)
            ->where('csl.booth_type', 1)
            ->where('csl.create_time', '>=', strtotime(date('Y-m-01')))
            ->group('csl.item_id')
            ->order('sale_num', 'desc')
            ->limit(20)
            ->select();
        foreach ($thisMonthCollectionSaleRank as &$item) {
            $item['item_icon'] = cmf_get_image_preview_url($item['item_icon']);
        }
        $this->assign('this_month_collection_sale_rank', $thisMonthCollectionSaleRank);
        // 本月商品销量排行榜
        $thisMonthProductSaleRank = $storeLogModel->alias('csl')
            ->leftJoin('cmf_store_item csi', 'csi.id = csl.item_id')
            ->field('sum(csl.item_num) as sale_num, csi.item_name, csi.item_icon, csi.id')
            ->where('csl.status', '>', 0)
            ->where('csl.booth_type', '<>', 1)
            ->where('csl.create_time', '>=', strtotime(date('Y-m-01')))
            ->group('csl.item_id')
            ->order('sale_num', 'desc')
            ->limit(20)
            ->select();
        foreach ($thisMonthProductSaleRank as &$item) {
            $item['item_icon'] = cmf_get_image_preview_url($item['item_icon']);
        }
        $this->assign('this_month_product_sale_rank', $thisMonthProductSaleRank);

        return $this->fetch('overview');
    }

    public function store(){
        //统计今日销售总额
        $today = date('Y-m-d');
        $time_today = strtotime($today.' 00:00:00');
        $time_7 = strtotime('-7 day');
        $day7 = date('Y-m-d', $time_7);
//        $day_month = date('Y-m-d', strtotime('last month'));
        //商城统计
        $storeLogModel = new StoreLogModel();
        $last_7 = $storeLogModel->field('COALESCE(sum(cost),0) sum,count(1) count,DATE(pay_time) date')->where('status',1)->where('create_time','>',$time_7)->group('date')->select();
        $last_7_u = $storeLogModel->field('DATE(pay_time) date,count(DISTINCT user_id) count')->where('status',1)->group('date')->select();
        $this->assign('last_7',$last_7);
        $day_0 = ['sum'=>0,'count'=>0,'unum'=>0];
        $day_7 = ['sum'=>0,'count'=>0,'unum'=>0];
        $week = [];
        foreach ($last_7 as $v){
            $v['unum'] = 0;
            if($v['date']==$today){
                $day_0 = $v;
            }
            
            $day_7['sum'] += $v['sum'];
            $day_7['count'] += $v['count'];
            $week[$v['date']] = $v;
        }
        $unum = $storeLogModel->field('count(DISTINCT user_id) count')->where('status',1)->find();
        $day_7['unum']=$unum['count'];
        foreach ($last_7_u as $u){
            $date= $u['date'];
            if(isset($week[$date])){
                $week[$date]['unum'] = $u['count'];
            }
        }
        $this->assign('day_0',$day_0);
        $this->assign('day_7',$day_7);
        //商城商品分类
        $item_sum = $storeLogModel->alias('log')->field('item_id,item_name,booth_type,sum(item_num) item_num,sum(cost) cost')
//                ->join('store_item it','it.id=log.item_id')
                ->where('create_time','>',$time_7)->where('status',1)->group('item_id')->select();
        $group_num = [1=>0,2=>0,3=>0];
        $group_cost = [1=>0,2=>0,3=>0];
        foreach ($item_sum as $item){
            $booth = $item['booth_type'];
            $group_num[$booth] += $item['item_num'];
            $group_cost[$booth] += $item['cost'];
        }
        $group_num_tt = 0;
        $group_cost_tt = 0;
        foreach ($group_num as $v){
            $group_num_tt += $v;
        }
        foreach ($group_cost as $v){
            $group_cost_tt += $v;
        }
        $group_num_tt = $group_num_tt<1?1:$group_num_tt;
        $group_cost_tt = $group_cost_tt<1?1:$group_cost_tt;
        $data  = [];
        $data[] = ['name'=>'数字藏品','value'=>round($group_num[1]/$group_num_tt,2)];
        $data[] = ['name'=>'美容院','value'=>round($group_num[2]/$group_num_tt,2)];
        $data[] = ['name'=>'零售区','value'=>round($group_num[3]/$group_num_tt,2)];
        $this->assign('sell_count', json_encode($data));
        $data  = [];
        $data[] = ['name'=>'数字藏品','value'=>round($group_cost[1]/$group_cost_tt,2)];
        $data[] = ['name'=>'美容院','value'=>round($group_cost[2]/$group_cost_tt,2)];
        $data[] = ['name'=>'零售区','value'=>round($group_cost[3]/$group_cost_tt,2)];
        $this->assign('sell_cost', json_encode($data));
        //任务统计
        $model = new DailyTaskModel();
        $task = $model->where('status_i','>',0)->where('date','>',$day7)->field('date,task_key,count(1) count')->group('date,task_key')->order('date asc')->select();
        $dates = [];
        $x_date_len = 7;
        for($i=$x_date_len-1;$i>0;$i--){
            $day = date('Y-m-d', strtotime('-'.$i. ' day'));
            $dates[] = $day;
        }
        $dates[] = $today;
        $aa = [];
        foreach ($task as $t){
//            $dates[] = $t['date'];
            $key = $t['task_key'];
            if(!isset($aa[$key])){
                $aa[$key] = [];
            }
        }
        $len = count($dates);
        foreach ($aa as $k =>$varr){
            for($i=0;$i<$len;$i++){
                $varr[] = 0;
            }
            $aa[$k] = $varr;
        }
        foreach ($dates as $idx => $date){
            foreach ($task as $t){
                if($t['date']==$date){
                    $key = $t['task_key'];
                    $aa[$key][$idx] = $t['count'];
                }
            }
        }
        $tasks = [];
        $namemap = ['OnlineTask'=>'在线时长','SignInTask'=>'每日签到','TalkAllTask'=>'文明发言','TalkTask'=>'文明私聊','CuriosityTask'=>'好奇宝宝'];
        foreach ($aa as $key=>$arr){
            
            $tasks[] = [
                'name'=>isset($namemap[$key])?$namemap[$key]:$key,
                'type'=> 'bar',
                'data'=> $arr
            ];
        }
        $this->assign('x_date_7',json_encode($dates));
        $this->assign('task_7',json_encode($tasks));
        
        
        
        //充值,提现
        $model = new UserScoreLogModel();
        $charge = $model->where('type','charge')->sum('score');
        $withdrawal = $model->where('type','withdrawal')->sum('score');
        $this->assign('charge',$charge);
        $this->assign('withdrawal',$withdrawal);
        $charge_log = $model->where('type','charge')->field('date(create_time) date,COALESCE(sum(score),0) sum')->where('create_time','>',$time_7)->select();
        $withdrawal_log = $model->where('type','withdrawal')->field('date(create_time) date,COALESCE(sum(score),0) sum')->where('create_time','>',$time_7)->select();
        $list = [];
        for($i=0;$i<$x_date_len;$i++){
            $date = $dates[$i];
            $score = 0;
            foreach ($charge_log as $log){
                if($log['date']==$date){
                    $score = $log['sum'];
                    break;
                }
            }
            $list[] = $score;
        }
        $this->assign('charge_7', json_encode($list));
        for($i=0;$i<$x_date_len;$i++){
            $date = $dates[$i];
            $score = 0;
            foreach ($withdrawal_log as $log){
                if($log['date']==$date){
                    $score = $log['sum'];
                    break;
                }
            }
            $list[] = $score;
        }
        $this->assign('withdrawal_7', json_encode($list));
        //分类
        $total_money = $model->field('type,sum(score) sum_score,sum(coin) sum_coin')->group('type')->select();
        $total_money_7 = $model->field('type,sum(score) sum_score,sum(coin) sum_coin')->group('type')->where('create_time','>',$time_7)->select();
        $total_money_today = $model->field('type,sum(score) sum_score,sum(coin) sum_coin')->group('type')->where('create_time','>',$time_today)->select();
        
        $map = xx_array_to_map($total_money, 'type');
        $map_7 = xx_array_to_map($total_money_7, 'type');
        $map_today = xx_array_to_map($total_money_today, 'type'); 
        foreach ($map as $k=>$v){
            if(!isset($map_7[$k])){
                $map_7[$k] = ['sum_score'=>0,'sum_coin'=>0];
            }
            if(!isset($map_today[$k])){
                $map_today[$k] = ['sum_score'=>0,'sum_coin'=>0];
            }
        }
        
        $this->assign('total_score_log',$map);
        $this->assign('total_score_log_7',$map_7);
        $this->assign('total_score_log_today',$map_today);
        //商品销售统计
        $model = new \api\mall\model\StoreModel();
        $data = $model->order('sell_count desc')->limit(0,10)->select();
        $this->assign('item_list',$data);
        return $this->fetch();
    }

}
